package com.qf.cqut.controller;

import com.qf.cqut.Service.FoodService;
import com.qf.cqut.Service.ReturnService;
import com.qf.cqut.model.pojo.AFood;
import com.qf.cqut.model.pojo.AReturn;
import com.qf.cqut.result.ResponseCode;
import com.qf.cqut.result.ResponseData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("returns")
@CrossOrigin("*")
public class ReturnController {

    @Resource
    private ReturnService returnService;

    @Resource
    private FoodService foodService;

    @RequestMapping("list")
    @ResponseBody
    public ResponseData<List<AReturn>> queryAll(Integer page, Integer limit){

        List<AReturn> AReturnList = returnService.selectByLimit(page,limit);
        Integer count = returnService.count();
        if(AReturnList.isEmpty()){
            return new ResponseData<>(ResponseCode.ERROR);
        }
        return new ResponseData<>(ResponseCode.SUCCESS,count,AReturnList);
    }

    @RequestMapping("del")
    @ResponseBody
    public ResponseData delById(String[] ids){
        Integer i = returnService.delByIds(ids);
        if(i == 0) {
            return new ResponseData(ResponseCode.ERROR);
        }else{
            return new ResponseData(ResponseCode.SUCCESS);
        }
    }

    @RequestMapping("add")
    @ResponseBody
    public ResponseData addAreturn(AFood aFood,AReturn aReturn){
        aReturn.setCreateTime(LocalDateTime.now());
        Integer i = returnService.addAreturn(aReturn);
        Integer j = foodService.executeAreturn1(aFood);
        if(i == 0 || j==0){
            return new ResponseData<>(ResponseCode.ERROR);
        }else{
            return new ResponseData<>(ResponseCode.SUCCESS);
        }
    }

    @RequestMapping("edit")
    @ResponseBody
    public ResponseData editAreturn(AFood aFood,AReturn aReturn){
        aReturn.setUpdateTime(LocalDateTime.now());
        Integer i = returnService.editAreturn(aReturn);
        Integer j = foodService.executeAreturn1(aFood);
        if(i == 0 || j==0){
            return new ResponseData<>(ResponseCode.ERROR);
        }else{
            return new ResponseData<>(ResponseCode.SUCCESS);
        }
    }
    @RequestMapping("conditions")
    @ResponseBody
    public ResponseData<List<AReturn>> queryByConditions(AReturn aReturn){
        List<AReturn> aReturnList = returnService.queryByConditions(aReturn);
        return new ResponseData<>(ResponseCode.SUCCESS,aReturnList);
    }

    @RequestMapping("execute")
    @ResponseBody
    public ResponseData executeAplan(AFood aFood, AReturn aReturn){
        aReturn.setCreateTime(LocalDateTime.now());
        Integer i = foodService.executeAreturn1(aFood);
        Integer j = returnService.addAreturn(aReturn);
        if(i == 0||j==0){
            return new ResponseData<>(ResponseCode.ERROR);
        }else{
            return new ResponseData<>(ResponseCode.SUCCESS);
        }
    }
}
